package com.neusoft.neuiotms.dcam.fixed.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.fixed.dto.StorehouseDTO;
import com.neusoft.neuiotms.dcam.fixed.repository.StorehouseRepository;
import com.neusoft.neuiotms.dcam.fixed.service.StorehouseService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "库房设置")
@RestController
@RequestMapping(value = "/api/w/fixed/storehouse")
public class StorehouseController {

    @Autowired
    private StorehouseService storehouseService;
    @Autowired
    private StorehouseRepository storehouseRepository;

    @ApiOperation("查询所有库房")
    @GetMapping
    public ResultListDTO<StorehouseDTO> index() {
        final List<StorehouseDTO> StorehouseDTOs = this.storehouseService.findAll();
        return ResultListDTO.success(StorehouseDTOs);
    }

    @ApiOperation("创建库房")
    @PostMapping
    public ResultDTO<StorehouseDTO> create(@RequestBody final StorehouseDTO dto) {
        final StorehouseDTO result = this.storehouseService.create(dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("库房详情")
    @GetMapping("/{id}")
    public ResultDTO<StorehouseDTO> details(@PathVariable final Long id) {
        final StorehouseDTO dto = this.storehouseService.details(id);
        return ResultDTO.success(dto);
    }

    @ApiOperation("删除库房")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.storehouseService.delete(id);
        return ResultDTO.success();
    }

    @ApiOperation("修改库房")
    @PutMapping("/{id}")
    public ResultDTO<StorehouseDTO> update(@PathVariable final Long id, @RequestBody final StorehouseDTO dto) {
        final StorehouseDTO result = this.storehouseService.update(id, dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("分页查询库房")
    @GetMapping("/p")
    public PageResultDTO<StorehouseDTO> search(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.storehouseService.search(pageable, searchable));
    }

    @ApiOperation("状态变更")
    @PutMapping("/statuschange/{id}/flag/{flag}")
    public ResultDTO<Void> statusChange(@PathVariable final Long id, @PathVariable final String flag) {
        this.storehouseService.statusChange(id, flag);
        return ResultDTO.success();
    }

    @ApiOperation("仓库数量统计")
    @GetMapping("/stats/total/{belongs}")
    public ResultDTO<?> totalStats(@PathVariable final String belongs) {
        final Long result = this.storehouseRepository.countByBelongs(belongs);
        return ResultDTO.success(result);
    }

}
